/**
 * 
 */
package com.android.hw.projectmenu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

import android.content.BroadcastReceiver;
import com.android.hw.projectmenu.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.TextView;

/**
 * @author gaoshengpan
 *
 */
public class BatteryInfo extends Activity {
	private Context mContext = this;
	private BatteryReceiver mbaBatteryReceiver ;
	private TextView tempTextView,voltageTxtTextView,currentTextView,manuTextView;
	private float temperature;
	private double voltage,current;
	private String mVoltage,mCurrent;
	private String TAG = "BatteryInfo";
	private IntentFilter ifilter ;
	private Intent batteryStatus;
	private Handler mHandler =new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			float temp = Float.valueOf(getBatteryTemp());
			temperature = temp / 10;
		    current =-(double)Integer.parseInt(readFile("sys/class/power_supply/bms/current_now"))/1000;
		    voltage  = (double)Integer.parseInt(readFile("/sys/class/power_supply/battery/voltage_now"))/(1000*1000);
            mCurrent = new DecimalFormat("0.00").format(current);
		    mVoltage = new DecimalFormat("0.00").format(voltage);
			tempTextView.setText(getString(R.string.battery_temperature)+temperature+"℃");
		    voltageTxtTextView.setText(getString(R.string.battery_voltage)+mVoltage+"V");
		    currentTextView.setText(getString(R.string.battery_current)+mCurrent+"mA");
		    mHandler.sendEmptyMessageDelayed(0, 1000);
		}
	};

	/**
	 * 
	 */
	public BatteryInfo() {
		// TODO Auto-generated constructor stub
	}
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.battery_info);
		tempTextView = (TextView)findViewById(R.id.textView2);
		voltageTxtTextView = (TextView)findViewById(R.id.textView3);
		currentTextView = (TextView)findViewById(R.id.textView4);
		manuTextView = (TextView)findViewById(R.id.textView5);
		mbaBatteryReceiver =new BatteryReceiver();
		
	}
	
	private class BatteryReceiver extends BroadcastReceiver {

		public void onReceive(Context mContext, Intent intent) {
		    float temp = Float.valueOf(getBatteryTemp());
			temperature = temp / 10;
			/*15-11-06 gaoshenpan fix battery info null begin */
			if(!"".equals(readFile("/sys/class/power_supply/battery/voltage_now"))){
		    	Log.d(TAG, "current---------->not null");
		    	current =-Integer.parseInt(readFile("sys/class/power_supply/bms/current_now"))/(1000*1000);
		    }else{
		    	current =current;
		    }
		    
		    if(!"".equals(readFile("/sys/class/power_supply/battery/voltage_now"))){
		    	Log.d(TAG, "voltage---------->not null");
		    	voltage  = Integer.parseInt(readFile("/sys/class/power_supply/battery/voltage_now"))/1000;
		    }else{
		    	voltage  = voltage;
		    }
		    /*15-11-06 gaoshenpan fix battery info null end */
		    String battery_manu = readFile("/sys/class/power_supply/bms/battery_type");
		    manuTextView.setText(getString(R.string.battery_manu)+battery_manu);
		    Log.d(TAG, "voltage---------->"+voltage+"\n"+"temperature---------->"+temperature);
		    Log.d(TAG, "battery_manu---------->"+battery_manu+"\n"+"battery_current---------->"+current);
		    mHandler.sendEmptyMessage(0);
		}
	}
	
	public String getBatteryTemp() {
		try {
			FileInputStream inStream = new FileInputStream(
					"/sys/class/power_supply/bms/temp");
			byte[] buffer = new byte[8];
			int nu = inStream.read(buffer);
			inStream.close();
			Log.e(TAG, "nu=" + nu);
			Log.e(TAG, "buffer.toString()=" + new String(buffer));
			return new String(buffer);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			// default test mode without usb_mode file.
			return "-1000";
		} catch (IOException e) {
			e.printStackTrace();
			return "-1000";
		}
	}
	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(mbaBatteryReceiver);
		mHandler.removeMessages(0);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		/*15-11-13 gaoshenpan fix receiver not register begin */
		ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		batteryStatus = this.registerReceiver(mbaBatteryReceiver, ifilter);
		/*15-11-13 gaoshenpan fix receiver not register end */
		mHandler.sendEmptyMessage(0);
	}
	private  String readFile(String filePath){   
		String res="";   
		try{   
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
			String str=null;
			while((str=br.readLine())!=null){
				res+=str;
			}  
			if(br !=null){
				br.close();
			}
        }   
		catch(Exception e){   
         e.printStackTrace();   
        }   
        return res;   
	}  

}
